##=============================================================================
## Appendix Figure 16
##=============================================================================

##-----------------
# clear environment
rm(list=ls())
options(stringsAsFactors = FALSE, scipen = 999)
# source("R/functions.R")

seed <- sample.int(.Machine$integer.max, 1)
set.seed(seed)

ipak <- function(pkg){new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if(length(new.pkg)) install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}

packages <- c("tidyverse", "hrbrthemes")

ipak(packages)

##---------
# Load data
#setwd("/Users/austinknuppe/Library/CloudStorage/Dropbox/Ukraine2022WartimeSurvey/Paper_peace/final_version_oct_2024/replication-scripts")
load("~/Desktop/clean_ukraine_data.RData")

dat3 <- dat %>% 
  mutate(wave2 = factor(wave, labels = c("July 2022 \n (n = 1,890)", 
                                         "February 2023 \n (n = 2,003)"))) %>% 
  mutate(frontline_oblast_2 = factor(frontline_b, 
                                  labels = c("Non-Frontline", "Frontline"))) %>%
  mutate(viina = case_when(
    viina_cum_adm2 == 0 ~ "0%",
    viina_cum_adm2 < 112 & viina_cum_adm2 > 0  ~ "25%",
    viina_cum_adm2 > 111 & viina_cum_adm2 < 594 ~ "50%",
    viina_cum_adm2 > 593 & viina_cum_adm2 < 2352 ~ "75%",
    viina_cum_adm2 > 2351 ~ "100%")) %>% 
  mutate(viina = factor(viina, levels = c("0%", "25%", "50%", "75%", 
                                          "100%"))) %>%  
  drop_na(compromise)

ggplot(dat3, aes(x = viina, y = compromise_idx_full_z, color = factor(wave2), 
                 group = factor(wave2))) +
  stat_summary(geom = "pointrange", fun.data = "mean_cl_boot", size = 1,
               linewidth = 2,
               position = position_jitterdodge(dodge.width = 0.5,
                                               seed = 1234)) +
  labs(x = "VIINA (Quantiles)", y = "Observed Probabilty of Compromise",
       color = "Wave") +
  scale_color_manual(values = c("gray60", "gray20")) +
  theme_set(theme_bw(base_size = 20)) +
  theme(legend.position = "bottom") 

rm(list = ls())
##=============================================================================
## End of File
##=============================================================================